﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using FlanProject.Combats.Actions;
using FlanProjectData.Characters;
using FlanProjectData.GameObjects;
using FlanProjectData.Helpers;
using FlanProjectData.Skills;
using FlanProjectData.Sprites;

namespace FlanProject.Combats
{
    public class CombatMonster : CombatCharacter
    {
        private static Logger logger = new Logger("Monster");

        public List<Item> DropItems { get; set; }

        [ContentSerializerIgnore]
        public Vector2 CollisionVector { get; set; }

        #region Fade Data

        private float alphaValue = 0;
        private float fadeIncrement = 0.01f;
        private double fadeDelay = 0.035;

        #endregion

        // Monster constructor
        public CombatMonster(Monster monster) : base()
        {
            if (monster == null)
                throw new ArgumentNullException("Unknown monster.");

            ArtificialIntelligence = new ArtificialIntelligence(this);
            DropItems = new List<Item>();

            Name = monster.Name;
            Stats = monster.BaseStats;
            IdleAnimation = monster.IdleAnimation;
            WalkAnimation = monster.WalkAnimation;
            AttackAnimation = monster.AttackAnimation;
            DeathAnimation = monster.DeathAnimation;
            DefenseAnimation = monster.DefenseAnimation;
            OnHitAnimation = monster.OnHitAnimation;
            OnStartLookDirection = monster.OnStartLookDirection;
            IsFloating = monster.IsFloating;
            HeightOffset = monster.HeightOffset;
        }

        // Load character animations, skills and move list
        public void LoadContent(ContentManager contentManager)
        {
            // Loads character animations and sets default animation
            IdleAnimation.LoadContent(contentManager);
            WalkAnimation.LoadContent(contentManager);
            AttackAnimation.LoadContent(contentManager);
            DeathAnimation.LoadContent(contentManager);
            OnHitAnimation.LoadContent(contentManager);
            DefenseAnimation.LoadContent(contentManager);
            CurrentAnimation.PlayAnimation(IdleAnimation);

            // Loads character skills
            SkillList = new List<Skill>();

            foreach (var skill in Skills)
            {
                var newSkill = contentManager.Load<Skill>(@"Data\Skills\" + skill);
                newSkill.LoadContent(contentManager);
                SkillList.Add(newSkill);
            }

            base.LoadContent();
        }

        // Update character
        public override void Update(GameTime gameTime)
        {
            HandleUserStates();

            if (CurrentState != States.Dead)
            {
                // Reset fade
                if (alphaValue != 0)
                    alphaValue = 0;

                // Choose action if none selected
                if (Action == null)
                {
                    Action = ArtificialIntelligence.ChooseAction();
                }
                else
                {
                    // If action returned, execute it
                    if (Action.Stage == CombatAction.CombatActionStage.NotStarted)
                        Action.Start();
                }
            }
            else
            {
                Fade(gameTime);
            }

            base.Update(gameTime);
        }

        // Draw current animation
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            // Flip the sprite to face the way we are moving
            if (Direction.X > 0)
            {
                if (OnStartLookDirection == MOVE_LEFT)
                    Flip = SpriteEffects.FlipHorizontally;
                else
                    Flip = SpriteEffects.None;
            }
            else if (Direction.X < 0)
            {
                if (OnStartLookDirection == MOVE_LEFT)
                    Flip = SpriteEffects.None;
                else
                    Flip = SpriteEffects.FlipHorizontally;
            }

            CurrentAnimation.Draw(spriteBatch, Flip, Position, gameTime, Color.Lerp(Color.White, Color.Transparent, alphaValue));
        }

        public void Fade(GameTime gameTime)
        {
            fadeDelay -= gameTime.ElapsedGameTime.TotalSeconds;

            if (fadeDelay <= 0)
            {
                fadeDelay = 0.035;
                alphaValue += fadeIncrement;
            }
        }

        public Item GetItemAt(int index)
        {
            Item i = null;
            if (index < DropItems.Count && index >= 0)
            {
                i = DropItems.ElementAt(index);
            }
            else
            {
                logger.Warn("No Item at this index : " + index);
            }
            return i;
        }

        public int CountDroppableItem()
        {
            int count;
            if (DropItems == null)
            {
                count = -1;
                logger.Warn("Drop Items Uninstanciated");
            }
            else
            {
                count = DropItems.Count;
            }
            return count;
        }
    }
}
